<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddInitialEpisodeCodeInPlaylistItemVersionTable extends AbstractMigration
{
    public function up()
    {
        $this->table('playlist_item_version')
            ->addColumn('initial_episode_code', 'string', [
                'limit' => 128,
                'default' => '',
                'null' => false,
                'comment' => '所属剧集'
            ])
            ->addIndex(['initial_episode_code'], ['name' => 'idx_initial_episode_code', 'type' => 'normal'])
            ->save();

        // 同步历史数据
        $this->execute("update playlist_item_version piv join entity e on piv.entity_id = e.id set piv.initial_episode_code = e.initial_episode_code");
    }

    public function down()
    {
        $this->table('playlist_item_version')->removeColumn('initial_episode_code')->save();
    }
}
